﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MechaShark
{
    class Camera
    {
        //-------------Members-------------
        public Matrix currentView;
        public Matrix currentProj;
        public Matrix currentWorld;
        public float cameraArc = 0;
        public float cameraRotation = 0;
        public float cameraDistance = 70;

        public float prevX = 30;
        public float prevY = 0;
        public float xpos = 30;
        public float ypos = 0;
        public float speed = 0.05f;

        //-------------Methods-------------

        public Camera()
        {

        }

        // update Camera
        #region camera update
        public bool Update(GameTime gameTime, GamePadState currentGamePadState)
        {
            float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            bool IsMoving = false;

            // Check for input to rotate the camera up and down around the model.

            xpos += currentGamePadState.ThumbSticks.Left.X * time * 0.10f;

            ypos += currentGamePadState.ThumbSticks.Left.Y * time * 0.10f;

            if (xpos >= 40)
                xpos = 40;
            else if (xpos <= -40)
                xpos = -40;

            if (ypos <= -20)
                ypos = -20;
            else if (25 <= ypos)
                ypos = 25;
            if ((xpos != prevX) && (ypos != prevY))
            {
                IsMoving = true;
            }
            prevX = xpos;
            prevY = ypos;

            return IsMoving;
        }
        #endregion

        // Compute camera matrices.
        public void ComputeMatricesToDraw(GraphicsDevice Device)
        {
            currentWorld =
               Matrix.CreateRotationY(MathHelper.ToRadians(270)) * 
               Matrix.CreateRotationX(MathHelper.ToRadians(cameraArc)) *
               Matrix.CreateTranslation(xpos, ypos, 0);

            currentView = Matrix.CreateRotationY(MathHelper.ToRadians(0)) *
            Matrix.CreateRotationX(MathHelper.ToRadians(0)) *
            Matrix.CreateTranslation(0, 0, -cameraDistance);

            currentProj = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(50),
                                                                    Device.Viewport.AspectRatio,
                                                                    1,
                                                                    1000);

        }
    }
}
